<?
session_start();
include("includes/session.php");
define("template", true);
$do = $_REQUEST['do'];
if($do == 'cover'){
	require_once('includes/pdf/config/lang/eng.php');
	require_once('includes/pdf/tcpdf.php');
	$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
	$pdf->setCreator(PDF_CREATOR);
	$pdf->setAuthor('Le Hong Phuong');
	$pdf->setTitle('To GCS');
	$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
	$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
	$pdf->setPrintHeader(false);
	$pdf->setPrintFooter(false);
	$pdf->setDefaultMonospacedFont(PDF_FONT_MONOSPACED);
	$pdf->SetMargins(30, 10, 30, true);
	$pdf->setHeaderMargin(PDF_MARGIN_HEADER);
	$pdf->setFooterMargin(PDF_MARGIN_FOOTER);
	$pdf->setAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
	$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
	$pdf->setLanguageArray($l);
	$cover = new stdClass;
	$cover->id = $_REQUEST['id'];
	$cover->soquyen = $_REQUEST['soquyen'];
	$cover->quyenso = $_REQUEST['quyenso'];
	$cover->tuquyen = $_REQUEST['tuquyen'];
	$cover->denquyen = $_REQUEST['denquyen'];
	$cover->tuso = $_REQUEST['tuso'];
	$cover->denso = $_REQUEST['denso'];
	$cover->name = $_REQUEST['name'];
	$cover->tax = $_REQUEST['tax'];
	$cover->address = $_REQUEST['address'];
	$cover->serial = $_REQUEST['serial'];
	$cover->form = $_REQUEST['form'];
	$cover->page = $_REQUEST['page'];
	if($cover->page == 'A4'){
		if($cover->quyenso!=0){
			$print = print_cover($cover->quyenso);
			include('models/'.$do.'.pdf.php');
		}
		elseif($cover->tuso!=0){
			if($cover->tuso%2!=1){
				echo '<h1>Số hóa đơn trước phải là số lẻ 1 (VD: 1,51,101,151,201)!<br></h1>';
				exit();
			}
			elseif($cover->tuso >= $cover->denso){
				echo '<h1>Làm gì có chuyện số hóa đơn trước lại to hơn số hóa đơn sau!</h1>';
				exit();
			}
			else{
				$i=($cover->tuso+49)/50;
				$k=$cover->denso/50;
				for($i;$i<=$k;$i++){
					$print = print_cover($i);
					include('models/'.$do.'.pdf.php');
				}
			}
		}
		elseif($cover->tuquyen!=0){
			if($cover->tuquyen > $cover->denquyen){
				echo '<h1>Làm gì có chuyện số quyển trước lại to hơn số quyển sau!</h1>';
				exit();
			}
			else{
				$i=$cover->tuquyen;
				$k=$cover->denquyen;
				for($i;$i<=$k;$i++){
					$print = print_cover($i);
					include('models/'.$do.'.pdf.php');
				}
			}
		}
		else{
			for($i=1;$i<=$cover->soquyen;$i++){
				$print = print_cover($i);
				include('models/'.$do.'.pdf.php');
			}
		}
	}
	else{
		if($cover->quyenso!=0)	{
			$print = print_cover($cover->quyenso);
			include('models/'.$do.'.pdf.php');
		}
		elseif($cover->tuso!=0){
			if($cover->tuso%2!=1){
				echo '<h1>Số hóa đơn trước phải là số lẻ 1 (VD: 1,51,101,151,201)!<br></h1>';
				exit();
			}
			elseif($cover->tuso >= $cover->denso){
				echo '<h1>Làm gì có chuyện số hóa đơn trước lại to hơn số hóa đơn sau!</h1>';
				exit();
			}
			else{
				$i=($cover->tuso+49)/50;
				$k=$cover->denso/50;
				for($i;$i<=$k;$i=$i+2){
					$print = print_cover($i);
					include('models/'.$do.'.pdf.php');
				}
			}
		}
		elseif($cover->tuquyen!=0){
			if($cover->tuquyen > $denquyen){
				echo '<h1>Làm gì có chuyện số quyển trước lại to hơn số quyển sau!</h1>';
				exit();
			}
			else{
				$i=$cover->tuquyen;
				$k=$cover->denquyen;
				for($i;$i<=$k;$i=$i+2){
					$print = print_cover($i);
					include('models/'.$do.'.pdf.php');
				}
			}
		}
		else{
			for($i;$i<=$k;$i=$i+2){
				$print = print_cover($i);
				include('models/'.$do.'.pdf.php');
			}
		}
	}
	$date=gmdate("Y-m-d h:i:s", time() + 3600*($timezone+date("0")));
	mysql_query("INSERT INTO `cover` (`pid`, `soquyen`, `quyenso`, `tuquyen`, `denquyen`, `tuso`, `denso`, `date`) VALUE ('$cover->id', '$cover->soquyen', '$cover->quyenso', '$cover->tuquyen', '$cover->denquyen', '$cover->tuso', '$cover->denso', '$cover->date')")or die("70 on post.inc.php");
	ob_end_clean();
	$filename = $cover->id.'-'.$do.'.pdf';
	$pdf->Output($filename, 'I');
}
else{
	setlocale(LC_MONETARY, 'en_US');
	$id = $_REQUEST['id'];
	$sql1 = mysql_query("select * from `contract` where `id` = '$id'") or die ('gọi Phương! Lỗi dòng số 17 file print.php');
	$sql2 = mysql_query("select * from `product` where `cid` = '$id' order by `order` asc") or die ('gọi Phương! Lỗi dòng số 18 file print.php');
	$sql3 = mysql_query("select * from `product` where `cid` = '$id' order by `order` asc") or die ('gọi Phương! Lỗi dòng số 19 file print.php');
	$sql4 = mysql_query("select * from `product` where `cid` = '$id' order by `order` asc") or die ('gọi Phương! Lỗi dòng số 20 file print.php');
	$sql5 = mysql_query("select * from `product` where `cid` = '$id' order by `order` asc") or die ('gọi Phương! Lỗi dòng số 21 file print.php');
	$rs = mysql_fetch_object($sql1);
	$rs2 = mysql_fetch_object($sql4);
	$name = $rs->name;
	$contract = $rs->contract;
	$tax = $rs->tax;
	$address = $rs->address;
	$date = date('d/m/Y', $rs->date);
	$director = $rs->director;
	$position = $rs->position;
	$niemphong = $rs->niemphong;
	$tamung = $rs->tamung;
	$ngaygiao = $rs->ngaygiao;
	$form = $rs2->form;
	$size = $rs2->size;
	$product = $rs->product;
	$datebb = explode('/',$rs->datebb);
	$sub_total = 0;
	$sohuy = @mysql_result(mysql_query("SELECT sum(sohuy) FROM `product` WHERE `cid` ='$id'"), 0, 0);
	$banhuy = @mysql_result(mysql_query("SELECT sum(banhuy) FROM `product` WHERE `cid` ='$id'"), 0, 0);
	if($do == 'phieuthu' && empty($_GET['sotien'])){
		$total = 0;
		while($st = mysql_fetch_object($sql2)){
			$sub_totaly = $st->quantity * $st->price;
			$sub_total = $sub_total + $sub_totaly;
			$tax_value = $sub_total * 10 / 100; 
			$total = $sub_total + round($tax_value);
		}
		echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<script>
			var text = prompt("xác nhận số tiền", '.$total.');
			document.location = "print?do=phieuthu&id='.$rs->id.'&pdf&sotien=" + text;
		</script>';
	}
	else{
		if(isset($_REQUEST['doc'])){
			$filename = $rs->id.'-'.$do.'.doc';
			header("Content-type: application/msword");
			header("Content-Disposition: attachment; Filename=$filename");
			include('models/'.$do.'.doc.php');
		}
		elseif(isset($_REQUEST['pdf'])){
			require_once('includes/pdf/config/lang/eng.php');
			require_once('includes/pdf/tcpdf.php');
			$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
			$pdf->setCreator(PDF_CREATOR);
			$pdf->setAuthor('Le Hong Phuong');
			$pdf->setTitle('To GCS');
			$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
			$pdf->setFooterFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_NAME_MAIN));
			$pdf->setPrintHeader(false);
			$pdf->setPrintFooter(false);
			$pdf->setDefaultMonospacedFont(PDF_FONT_MONOSPACED);
			$pdf->setHeaderMargin(PDF_MARGIN_HEADER);
			$pdf->setFooterMargin(PDF_MARGIN_FOOTER);
			$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
			$pdf->setLanguageArray($l);
			if(($rs->datebb!='')||(($rs->datebb=='')&&(($do=='hopdong')||($do=='phieuthu')))){
				include('models/'.$do.'.pdf.php');
				ob_end_clean();
				$filename = $rs->id.'-'.$do.'.pdf';
				$pdf->Output($filename, 'I');
			}
			else{
			$doc_url = rtrim($_SERVER['QUERY_STRING'], '&amp;pdf');
				$html = '<center><br /><br /><br /><br />
				<form action="home.php" method="post">
				  <input type="hidden" name="id" value="'.$id.'">
				  <input type="hidden" name="menu" value="add">
				  Nhập ngày biên bản: <input type="text" size="12" name="datebb" value="'.date('d/m/Y').'">
				  <input type="submit" value="Nhập" name="submitbb">
				  </form>
				  <a href="?'.$doc_url.'&doc">Hoặc bấm đây để tải về file DOC</a>
				';
				echo $html;
			}
		}
	}
}
?>